package pro.huadi.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pro.huadi.mapper.UserMapper;
import pro.huadi.pojo.User;
import pro.huadi.util.Result;

@RestController
@CrossOrigin
@RequestMapping
public class UserController {
    @Autowired
    UserMapper userMapper;

    //测试连通
    @GetMapping("/")
    public void test(){
        User user=new User("1","admin","kun");
       // System.out.println("haha");
        System.out.println(userMapper);
        System.out.println(user);
        //System.out.println(userMapper.test());
        System.out.println(userMapper.query(user));
        //userMapper.add(user);
        //return 111;
    }

    //登录
    @PostMapping(value = {"/login"},produces = {"application/json; charset=UTF-8"})
    public Result login(@RequestBody User user){
        User temp=new User();
        temp=userMapper.query(user);
        if(temp==null){
            return Result.error().code(2001);//2001表示没有该用户
        }
        else if(!temp.getPwd().equals(user.getPwd())){
            return Result.error().code(2002);//2002表示密码不正确
        }
        System.out.println("haha");
        return Result.ok().data("user",user);
    }


    //注册
    @PostMapping(value = {"/register"},produces = {"application/json; charset=UTF-8"})
    public Result register(@RequestBody User user){
        //System.out.println(user);
        User temp=new User();
        temp=userMapper.query(user);
        if(temp!=null){
            return Result.error().code(2001);//2001表示该用户已存在
        }
        userMapper.add(user);
        return Result.ok();
    }
}
